package com.huike.report.service.impl;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.huike.common.core.domain.entity.SysUser;
import com.huike.report.domain.vo.SalesStatisticVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.vo.IndexStatisticsVo;
import com.huike.clues.mapper.SysDeptMapper;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.clues.mapper.TbActivityMapper;
import com.huike.clues.mapper.TbAssignRecordMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.common.core.domain.entity.SysDept;
import com.huike.contract.mapper.TbContractMapper;
import com.huike.report.domain.vo.IndexVo;
import com.huike.report.mapper.ReportMapper;
import com.huike.report.service.IReportService;

@Service
public class ReportServiceImpl implements IReportService {

    @Autowired
    private TbContractMapper contractMapper;


    @Autowired
    private SysDictDataMapper sysDictDataMapper;


    @Autowired
    private TbClueMapper clueMapper;

    @Autowired
    private TbActivityMapper activityMapper;

    @Autowired
    private TbBusinessMapper businessMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private TbAssignRecordMapper assignRecordMapper;

    @Autowired
    private ReportMapper reportMpper;


    @Override
    public IndexVo getIndex(IndexStatisticsVo request) {
        Long deptId = request.getDeptId();
        TbAssignRecord tbAssignRecord = new TbAssignRecord();
        tbAssignRecord.setLatest("1");
        assignRecordMapper.selectAssignRecordList(tbAssignRecord);
        return null;
    }

    @Override
    public List<Map<String, Object>> salesStatisticsForIndex(IndexStatisticsVo request) {
        List<Map<String, Object>> list = contractMapper.contractStatisticsByUser(request);
        for (Map<String, Object> datum : list) {
            Long deptId = (Long) datum.get("dept_id");
            if (deptId != null) {
                SysDept dept = deptMapper.selectDeptById(deptId);
                datum.put("deptName", dept.getDeptName());
            }
        }
        return list;
    }

    //查询线索转换漏斗
    @Override
    public HashMap<String, Integer> getReportMap(String beginCreateTime, String endCreateTime) {
        //查询线索数
        IndexStatisticsVo indexStatisticsVo = new IndexStatisticsVo();
        indexStatisticsVo.setBeginCreateTime(beginCreateTime);
        indexStatisticsVo.setEndCreateTime(endCreateTime);

        //线索数
        Integer cluesNums = clueMapper.getCluesNums(indexStatisticsVo);
        //查询有效线索数
        Integer effectiveCluesNums = clueMapper.getCluesNum(indexStatisticsVo, LocalDate.now().toString(), null);
        //查询商机数
        Integer businessNums = businessMapper.countAllBusinessForIndex(indexStatisticsVo);
        //合同数量
        Integer contractNums = contractMapper.contractNums(indexStatisticsVo);

        HashMap<String, Integer> map = new HashMap<>();
        map.put("cluesNums", cluesNums);
        map.put("effectiveCluesNums", effectiveCluesNums);
        map.put("businessNums", businessNums);
        map.put("contractNums", contractNums);


        return map;
    }

    //线索龙虎榜
    @Override
    public ArrayList<SalesStatisticVo> salesDesc(String beginCreateTime, String endCreateTime) {
        //创建集合返回数据
        ArrayList<SalesStatisticVo> salesStatisticVos = new ArrayList<>();
        //获取用户信息
        ArrayList<Map<String, Object>> userList = clueMapper.desc(beginCreateTime, endCreateTime);
        //获取有效线索数量
        IndexStatisticsVo indexStatisticsVo = new IndexStatisticsVo();
        indexStatisticsVo.setBeginCreateTime(beginCreateTime);
        indexStatisticsVo.setEndCreateTime(endCreateTime);
        SalesStatisticVo salesStatisticVo = new SalesStatisticVo();

        for (Map<String, Object> map : userList) {
            String createby = (String) map.get("create_by");
           Long num = (Long) map.get("num");//线索数量

            //获取有效线索数量
            Integer zhuanHuanLu = clueMapper.getCluesNum(indexStatisticsVo, LocalDate.now().toString(), createby);
            //查询用户表
            salesStatisticVo.setCreateBy(createby);//用户名称
            Long deptId = clueMapper.getUser(createby);
                salesStatisticVo.setDepId(deptId);//部门ID
                //获取部门数据
                SysDept sysDept = clueMapper.getDeptName(deptId);
                salesStatisticVo.setDeptName(sysDept.getDeptName());//部门名称

            if (num != 0 && zhuanHuanLu != 0) {
                salesStatisticVo.setNum(num);//转化条数
                //算出转化率
                BigDecimal radio = getRadio(zhuanHuanLu, num);
                salesStatisticVo.setRadio(radio);//转化率
            }
            //存入集合
            salesStatisticVos.add(salesStatisticVo);
        }
        return salesStatisticVos;
    }

    private BigDecimal getRadio(Integer all, Long num) {
        if (all.intValue() == 0) {
            return new BigDecimal(0);
        }
        BigDecimal numBigDecimal = new BigDecimal(num);
        BigDecimal allBigDecimal = new BigDecimal(all);
        BigDecimal divide = numBigDecimal.divide(allBigDecimal, 4, BigDecimal.ROUND_HALF_UP);
        return divide.multiply(new BigDecimal(100));
    }

}